/*
 * @lc app=leetcode id=240 lang=cpp
 *
 * [240] Search a 2D Matrix II
 */
class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int row = matrix.size();
        if (row < 1) {
            return false;
        }
        int col = matrix[0].size();
        if (col < 1) {
            return false;
        }

        int r = 0;
        int c = col-1;
        while(r < row && c >= 0) {
            if (matrix[r][c] == target) {
                return true;
            }

            matrix[r][c] > target ? c-- : r++;
        }
        return false;
    }
};

